# Maintainer: Caleb Maclennan <caleb@alerque.com>
# Contributor: Ilaï Deutel

pkgname=typst
pkgver=0.9.0
pkgrel=1
epoch=1
pkgdesc='A markup-based typesetting system for the sciences'
arch=(loong64 x86_64)
url="https://$pkgname.app"
_url="https://github.com/$pkgname/$pkgname"
license=(Apache)
depends=(gcc-libs
         glibc)
makedepends=(cargo
             git)
_archive="$pkgname-$pkgver"
source=("$_url/archive/v$pkgver/$_archive.tar.gz")
sha256sums=('741256f4f45c8979c9279fa5064a539bc31d6c65b7fb41823d5fa9bac4821c01')

prepare() {
	cd "$_archive"
	cargo fetch --locked --target "$CARCH-unknown-linux-gnu"
}

build() {
	local hash=$(gunzip < "$_archive.tar.gz" | git get-tar-commit-id)
	cd "$_archive"
	CFLAGS+=" -ffat-lto-objects"
	export TYPST_VERSION="$pkgver (${hash::8})"
	export GEN_ARTIFACTS=artifacts/
	cargo build -p typst-cli --frozen --release --all-features
}

check() {
	cd "$_archive"
	cargo test --workspace --frozen --all-features
}

package() {
	cd "$_archive"
	local _artifacts='crates/typst-cli/artifacts'
	install -Dm0755 -t "$pkgdir/usr/bin/" "target/release/$pkgname"
	install -Dm0644 -t "$pkgdir/usr/share/man/man1/" "$_artifacts/$pkgname"*.1
	install -Dm0644 -t "$pkgdir/usr/share/zsh/site-functions/" "$_artifacts/_$pkgname"
	install -Dm0644 -t "$pkgdir/usr/share/fish/vendor_completions.d/" "$_artifacts/$pkgname.fish"
	install -Dm0644 "$_artifacts/$pkgname.bash" "$pkgdir/usr/share/bash-completion/completions/$pkgname"
}
